package com.calis.ill.server.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.calis.ill.order.service.api.OrderService;
import com.calis.ill.order.service.api.vo.OrderItemVO;
import com.calis.ill.order.service.api.vo.OrderVO;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping("/{userId}/order")
public class OrderController {
	
	@Autowired
	OrderService orderService;
	
	ObjectMapper mapper = new ObjectMapper();

	@PostMapping("/create")
	public void create(@PathVariable String userId, @RequestBody String ordeItems) throws Exception{
		List<OrderItemVO> items = mapper.readValue(ordeItems, new TypeReference<List<OrderItemVO>>(){});
		orderService.create(userId,items);
	};
	
	@GetMapping
	public String myOrders(@PathVariable String userId) throws Exception{
		List<OrderVO> orders = orderService.getMyOrders(userId);
		String rst = mapper.writeValueAsString(orders);
		return rst;
	};
}
